package com.liu.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.liu.sunnyday.constant.MessageConstant;
import com.liu.sunnyday.entity.PageResult;
import com.liu.sunnyday.entity.QueryPageBean;
import com.liu.sunnyday.entity.Result;
import com.liu.sunnyday.pojo.CheckGroup;
import com.liu.sunnyday.pojo.Setmeal;
import com.liu.sunnyday.service.SetmealService;
import com.liu.sunnyday.utils.QiNiuUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * chance！Time
 *
 * @author liuqian
 * @date 2022-06-14 15:26
 * *
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Reference
    private SetmealService setmealService;

    /**
     * 1-分页查询所有套餐
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        //调用服务分页查询
        PageInfo<Setmeal> pageInfo = setmealService.findPage(queryPageBean);
        PageResult pageResult = new PageResult(pageInfo.getTotal(), pageInfo.getList());
        return new Result(true, MessageConstant.GET_SETMEAL_LIST_SUCCESS, pageResult);
    }

    /**
     * 2-1新增套餐时，查询检查项
     * 3-2编辑套餐时，查询所有检查组
     */
    @GetMapping("/findAllgroup")
    public Result findAllgroup() {
        List<CheckGroup> list = setmealService.findAllgroup();
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, list);
    }

    /**2-2新增套餐：套餐上传图片
     * upload不是在script方法传来的，请找图片上传组件
     * 七牛云工具类
     * @param imgFile
     * @return
     */
    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile) {
        //System.out.println("进入上传方法upload");
        //    获取原有图片名称，截取到后缀名
        String originalFilename = imgFile.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));

        //- 生成唯一文件名，拼接后缀名
        String filename = UUID.randomUUID() + extension;
//- 调用七牛上传文件
        try {
            QiNiuUtils.uploadViaByte(imgFile.getBytes(), filename);
            Map<String, String> map = new HashMap<String, String>();
            map.put("imgName", filename);
            map.put("domain", QiNiuUtils.DOMAIN);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, map);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
    }

    /*2-3新增套餐，点击确定*/
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        setmealService.add(setmeal, checkgroupIds);
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 3-1编辑修改操作：查询套餐回显
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id) {
        //调用查询
        Setmeal setmeal = setmealService.findById(id);
        //图片需要全路径
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("setmeal", setmeal);
        resultMap.put("domain", QiNiuUtils.DOMAIN);
        //System.out.println(resultMap+"findBYid   ");
        return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, resultMap);
    }

    /**
     * 3-3编辑套餐：查询复选框选中的检查组,根据套餐id查询，方便返回去回写,然后修改
     * @param id
     * @return
     */
    @GetMapping("/findCheckgroupIdsBySetmealId")
    public Result findCheckgroupIdsBySetmealId(Integer id) {

        List<Integer> checkgroupids = setmealService.findCheckgroupIdsBySetmealId(id);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, checkgroupids);
    }

    //3-4编辑套餐：点击确定提交数据
    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        setmealService.update(setmeal, checkgroupIds);
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
    }
    //4删除套餐
    @PostMapping("/deleteById")
    public Result deleteById(Integer id) {
        setmealService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

}
